home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QuickDraw GX / GX->PostScript Sample / GXToPostScript / TestGXToPostScript.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  12.6 KB  |  520 lines  |  [TEXT/CWIE]

  1. /*
  2.      File:        TestGXToPostScript.cp
  3.  
  4.      Contains:    QuickDraw GX to PostScript conversion code.
  5.  
  6.      Version:    Technology:    Quickdraw GX 1.1.x
  7.       
  8.      Copyright:    ©1997 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. #include <Errors.h>
  12. #include <Files.h>
  13. #include <GXGraphics.h>
  14. #include <StorageLibrary.h>
  15. #include <GraphicsLibraries.h>
  16.  
  17. #include "GXPrintingUniverse.h"
  18. #include <Dialogs.h>
  19. #include <Controls.h>
  20. #include <StandardFile.h>
  21.  
  22.  
  23. #define kPreviewItem                4
  24. #define    kNoPreview                   1        
  25. #define    kBlackAndWhitePreview        2
  26. #define    k8BitPreview                3
  27. #define    k32BitPreview                4
  28.  
  29. #define kLevelFirstItem            5
  30. #define kLevelButtonCount        2
  31. #define kPSLevel1                1
  32. #define kPSLevel2                2
  33.  
  34.  
  35. #define kFontFirstItem            8
  36. #define kFontButtonCount        2
  37. #define kPSOnlyFonts            1
  38. #define kPSPlusTTFonts            2
  39.  
  40.  
  41. #define kColorFirstItem         11
  42. #define kColorButtonCount        3
  43. #define kGrayColors                1
  44. #define kRGBColors                2
  45. #define kCMYKColors                3
  46.  
  47.  
  48. #define kPointFirstItem     15
  49. #define kPointButtonCount    3
  50. #define    k1500points           1
  51. #define    k3000points            2
  52. #define    k10000points        3
  53.  
  54.  
  55. #define kDataFirstItem            19
  56. #define kDataButtonCount        2
  57. #define kAscii                    1
  58. #define    kBinary                    2
  59.  
  60. #define kVMItem                    22
  61. #define k250kb                    1
  62. #define k500kb                    2
  63. #define k1000kb                    3
  64. #define k2000kb                    4
  65. #define kUnlimittedVM            5
  66.  
  67. typedef struct {
  68.  
  69.     long            version;
  70.     long            previewType;
  71.     long            languageLevel;
  72.     long            fontFormats;
  73.     long            colorSpace;
  74.     long            pointLimit;
  75.     long            dataFormat;
  76.     long            maxVM;
  77.  
  78. } TepsSettings;
  79.  
  80. /*****************************************
  81.  
  82.     SetRadioButtonOn:
  83.     
  84.     You'd think there would be a routine like
  85.     this in the tool box.  What a pain.
  86.     
  87.     Oh well.
  88.     
  89.     Routine turns on a radio button in the specified
  90.     range, and turns off the others.
  91.     
  92.     dPtr:                Dialog pointer.
  93.     firstItem:    index of first item in group of radio buttons. (index of staticText item
  94.                                                         right before buttons)
  95.     itemCount:    How many radio buttons in group.
  96.     value:            Which one to turn on.
  97.     
  98. *******************************************/
  99. void SetRadioButtonOn( DialogPtr dPtr, short firstItem, short itemCount, short value);
  100. void SetRadioButtonOn( DialogPtr dPtr, short firstItem, short itemCount, short value)
  101.     {
  102.         short                                idx;    
  103.         short                                itemType;
  104.         Rect                                itemRect;
  105.         Handle                            hItem;
  106.  
  107.         for (idx = 1; idx <= itemCount; ++idx) {
  108.         
  109.             GetDialogItem(dPtr, firstItem + idx, &itemType, &hItem, &itemRect);
  110.             
  111.             if (idx != value)
  112.                 SetControlValue((ControlHandle)hItem, 0);
  113.             else
  114.                 SetControlValue((ControlHandle)hItem, 1);
  115.         
  116.         }//end for
  117.         
  118.     }//SetRadioButtonOn
  119.  
  120. /************************************************
  121.  
  122.     HandleSettingsDialog:
  123.     
  124.     Routine controls the DTP dialog for setting up
  125.     the extension options.
  126.     
  127. *************************************************/
  128. void    HandleSettingsDialog(DialogPtr dPtr, short *hit, TepsSettings *useSettings);
  129. void    HandleSettingsDialog(DialogPtr dPtr, short *hit, TepsSettings *useSettings)
  130.     {
  131.         OSErr                            status;
  132.         short                            itemType;
  133.         Rect                                itemRect;
  134.         Handle                            hItem;
  135.         TepsSettings                    settings, newSettings;
  136.         short                            itemHit;
  137.         
  138.         settings.version = 0;        
  139.         settings.previewType = kNoPreview;
  140.         settings.languageLevel = kPSLevel1;
  141.         settings.fontFormats = kPSOnlyFonts;
  142.         settings.colorSpace = kRGBColors;
  143.         settings.pointLimit = k1500points;
  144.         settings.dataFormat = kAscii;
  145.         settings.maxVM = k250kb;
  146.  
  147.         newSettings = settings;
  148.         
  149.         /** Initialize settings based on what's stored in DTP **/
  150.         
  151.         // Preview popup.
  152.         GetDialogItem(dPtr, kPreviewItem, &itemType, &hItem, &itemRect);
  153.         SetControlValue((ControlHandle)hItem, settings.previewType);
  154.         
  155.         // VM Usage popup.
  156.         GetDialogItem(dPtr, kVMItem, &itemType, &hItem, &itemRect);
  157.         SetControlValue((ControlHandle)hItem, settings.maxVM);
  158.         
  159.         // The rest.        
  160.         SetRadioButtonOn( dPtr, kLevelFirstItem , kLevelButtonCount, newSettings.languageLevel);
  161.         SetRadioButtonOn( dPtr, kFontFirstItem , kFontButtonCount,     newSettings.fontFormats);
  162.         SetRadioButtonOn( dPtr, kColorFirstItem , kColorButtonCount, newSettings.colorSpace);
  163.         SetRadioButtonOn( dPtr, kPointFirstItem , kPointButtonCount, newSettings.pointLimit);
  164.         SetRadioButtonOn( dPtr, kDataFirstItem , kDataButtonCount, newSettings.dataFormat);
  165.         
  166.         do {         /* Loop for dialog handling */
  167.         
  168.             ModalDialog(nil, hit);
  169.             
  170.             itemHit = *hit;
  171.             
  172.             if ( itemHit == kPreviewItem ) {
  173.             
  174.                 GetDialogItem(dPtr, kPreviewItem, &itemType, &hItem, &itemRect);
  175.                 newSettings.previewType = GetControlValue((ControlHandle)hItem);
  176.                 
  177.             } else if (itemHit == kVMItem) {
  178.             
  179.                 GetDialogItem(dPtr, kVMItem, &itemType, &hItem, &itemRect);
  180.                 newSettings.maxVM = GetControlValue((ControlHandle)hItem);
  181.                 
  182.             } else if ( (itemHit > kLevelFirstItem) && ( itemHit <= kLevelFirstItem + kLevelButtonCount) ) {
  183.  
  184.                 newSettings.languageLevel = itemHit - kLevelFirstItem;            
  185.                 SetRadioButtonOn( dPtr, kLevelFirstItem, kLevelButtonCount, newSettings.languageLevel);
  186.                 
  187.  
  188.             }    else if ( (itemHit > kFontFirstItem) && ( itemHit <= kFontFirstItem + kFontButtonCount) ) {
  189.             
  190.                 newSettings.fontFormats = itemHit - kFontFirstItem;
  191.                 SetRadioButtonOn( dPtr, kFontFirstItem, kFontButtonCount, newSettings.fontFormats);
  192.         
  193.  
  194.             } else if ( (itemHit > kColorFirstItem) && ( itemHit <= kColorFirstItem + kColorButtonCount) ) {
  195.             
  196.                 newSettings.colorSpace = itemHit - kColorFirstItem;
  197.                 SetRadioButtonOn( dPtr, kColorFirstItem, kColorButtonCount, newSettings.colorSpace);
  198.         
  199.  
  200.             } else if ( (itemHit > kPointFirstItem) && ( itemHit <= kPointFirstItem + kPointButtonCount) ) {
  201.             
  202.                 newSettings.pointLimit = itemHit - kPointFirstItem;
  203.                 SetRadioButtonOn( dPtr, kPointFirstItem, kPointButtonCount, newSettings.pointLimit);
  204.         
  205.  
  206.             } else if ( (itemHit > kDataFirstItem) && ( itemHit <= kDataFirstItem + kDataButtonCount) ) {
  207.             
  208.                 newSettings.dataFormat = itemHit - kDataFirstItem;
  209.                 SetRadioButtonOn( dPtr, kDataFirstItem, kDataButtonCount, newSettings.dataFormat);
  210.                 
  211.             }//end if
  212.         
  213.         } while ( (itemHit != ok) && (itemHit != cancel));
  214.         
  215.         *useSettings = newSettings;
  216.         
  217.     }//HandleSettingsDialog
  218.  
  219.  
  220. /************************************************************************************/
  221.  
  222. Boolean DoSettingsDialog(gxPostScriptImageDataRec *imageData);
  223. Boolean DoSettingsDialog(gxPostScriptImageDataRec *imageData)
  224. {
  225.     DialogRef    dlog;
  226.     short            hit;
  227.     short            oldResFile;
  228.     GrafPtr        oldPort;
  229.     TepsSettings        theSettings;
  230.     
  231.     GetPort(&oldPort);
  232.  
  233.     dlog = GetNewDialog(128, nil, (WindowRef)-1);
  234.     
  235.     SetDialogDefaultItem(dlog, ok);
  236.     SetDialogCancelItem(dlog, cancel);
  237.  
  238.     ShowWindow((WindowRef)dlog);
  239.     SetPort(dlog);
  240.  
  241.     HandleSettingsDialog(dlog, &hit, &theSettings);
  242.  
  243.     SetPort(oldPort);
  244.  
  245.     DisposeDialog(dlog);
  246.  
  247.  
  248.     OSErr                                            status;
  249.     gxPostScriptRenderOptions            options;
  250.     scalerStreamTypeFlag                    streamTypes;
  251.     gxPostScriptImageDataPtr            pImageData;
  252.  
  253.     options = gxEPSTargetOption;
  254.  
  255.     streamTypes = type1StreamType + 
  256.                                 type3StreamType + 
  257.                                 portableStreamType +
  258.                                 evenOddModifierStreamType;
  259.     
  260.     if (theSettings.dataFormat == kAscii)
  261.         options += gxNeedsHexOption;
  262.         
  263.     if (theSettings.fontFormats == kPSPlusTTFonts)
  264.         streamTypes += type42StreamType;
  265.         
  266.     /** Get Language Level **/
  267.     if (theSettings.languageLevel == kPSLevel1) {
  268.         imageData->languageLevel = 1;
  269.         options += gxPortablePostScriptOption;
  270.     } else if (theSettings.languageLevel == kPSLevel2) {
  271.         imageData->languageLevel = 2;
  272.     }//end if
  273.                         
  274.  
  275.     imageData->renderOptions = options;
  276.     imageData->fontType = streamTypes;
  277.  
  278.     /** Deal with color fields **/        
  279.     imageData->devCProfile = nil;
  280.  
  281.     switch (theSettings.colorSpace) {
  282.     
  283.         case kGrayColors:
  284.             imageData->devCSpace = gxGraySpace;
  285.             break;
  286.             
  287.         case kRGBColors:
  288.             imageData->devCSpace = gxRGBSpace;
  289.             break;
  290.             
  291.         case kCMYKColors:
  292.             imageData->devCSpace = gxCMYKSpace;
  293.             break;
  294.         
  295.     }//end switch
  296.     
  297.     /** Deal with path limit **/
  298.     switch (theSettings.pointLimit) {
  299.  
  300.         case k1500points:
  301.             imageData->pathLimit = 1500;
  302.             break;
  303.             
  304.         case k3000points:
  305.             imageData->pathLimit = 3000;
  306.             break;
  307.         
  308.         case k10000points:
  309.             imageData->pathLimit = 10000;
  310.             break;
  311.  
  312.     }//end switch
  313.     
  314.     /** Deal with maxVM **/
  315.     switch (theSettings.maxVM) {
  316.     
  317.         case k250kb:
  318.             imageData->printerVM = 250 * 1024;
  319.             break;
  320.             
  321.         case k500kb:
  322.             imageData->printerVM = 500 * 1024;
  323.             break;
  324.             
  325.         case k1000kb:
  326.             imageData->printerVM = 1000 * 1024;
  327.             break;
  328.             
  329.         case k2000kb:
  330.             imageData->printerVM = 2000 * 1024;
  331.             break;
  332.             
  333.         case kUnlimittedVM:
  334.             imageData->printerVM = 0x7FFFFFFF;
  335.             break;
  336.     
  337.     }//end switch
  338.  
  339.  
  340.     /* Remaining things are hard-coded */
  341.     
  342.     imageData->gsaveLimit = 25;                        // Allow 6 for app embedding EPS file.
  343.     imageData->opStackLimit = 450;                    // Allow 50 for app embedding EPS file.
  344.     
  345.     return (hit == ok) ? true : false;
  346.  
  347. }
  348.  
  349.  
  350. gxShape ShapeRead();
  351. gxShape ShapeRead()
  352.     {
  353.         OSErr status;
  354.         gxShape        result = nil;
  355.         StandardFileReply         reply;
  356.         OSType            theTypes[1] = {'qdgx'};
  357.         GrafPtr            thecurPort;
  358.         GetPort(&thecurPort);
  359.  
  360.         StandardGetFile(nil, 1, theTypes, &reply);
  361.         
  362.         SetPort(thecurPort);
  363.                 
  364.         if (reply.sfGood) {
  365.         
  366.             short refnum;
  367.             status = FSpOpenDF(&reply.sfFile, fsRdPerm, &refnum);
  368.             
  369.             result = FRefToShape(refnum, 0, nil);
  370.             
  371.             status = FSClose(refnum);
  372.             
  373.         }//end if
  374.                     
  375.         return(result);
  376.         
  377.     }//ShapeRead
  378.     
  379.     
  380.  
  381. #include <Stdio.h>
  382. #include <Files.h>
  383. #include "GXToPostScript.h"
  384. #include "IOUtilities.h"
  385.  
  386. // Define a subclass of the PostScript device for our test I/O
  387. class testDevice : public CGXtoPostScriptDevice {
  388.  
  389.     public:
  390.         testDevice(short refNum) {mRefnum = refNum; mSize = 0; mTotalSize = 0;};
  391.         virtual             ~testDevice() {
  392.             mTotalSize += mSize; 
  393.             printf("Done, total file size: %ld\n", mTotalSize);
  394.         };
  395.         
  396.         virtual            OSErr        BufferData(char* data, long size, unsigned long flags)
  397.             {
  398.                 OSErr    status = noErr;
  399.                 char* outputData = data;
  400.                 if (flags & gxMakeBufferHex) {
  401.                     outputData = new char[size * 2];
  402.                     HexBlockMove((unsigned char*)data, (unsigned char*)outputData, size);
  403.                     size *= 2;
  404.                 }//end if
  405.  
  406.                 long count = size;
  407.                 status = FSWrite(mRefnum, &count, outputData);
  408.                 mSize += count;
  409.                 mTotalSize += count;
  410.                 if (mSize > 10240) {
  411.                     mSize = 0;
  412.                     printf("Wrote %ld bytes\n", mTotalSize);
  413.                 }//end if
  414.  
  415.                 if (outputData != data)
  416.                     delete [] outputData;
  417.  
  418.                 return status;
  419.  
  420.             };
  421.  
  422.         virtual OSErr Idle() {return noErr;};
  423.  
  424.         long            GetSize() {return mTotalSize;};
  425.             
  426.     protected:
  427.         short        mRefnum;
  428.         long            mSize, mTotalSize;
  429. };
  430.  
  431. short        DoSave();
  432. short        DoSave()
  433.     {
  434.         SFReply         reply;
  435.         OSErr            status = noErr;
  436.         Point                where = {50, 50};
  437.         SFPutFile(where, "\pSave Garnet Stream", "\pTest.ps", nil, &reply);
  438.         short refnum;
  439.         if (reply.good) {
  440.         
  441.             status = Create(reply.fName, reply.vRefNum, 'MPS ', 'TEXT');
  442.             if ((status == noErr) || (status == dupFNErr)) {
  443.                 status = FSOpen(reply.fName, reply.vRefNum, &refnum);
  444.             }//end if
  445.         }//end if
  446.  
  447.         if (status != noErr)
  448.             throw status;
  449.             
  450.         return refnum;
  451.     }
  452.     
  453. main() {
  454.  
  455. // This ensures that new throws an exception if there is no space.
  456.     extern char            __throws_bad_alloc;
  457.     long                    throwvalue = 1;
  458.     __throws_bad_alloc = throwvalue;    
  459.  
  460.     InitGraf(&qd.thePort); InitFonts(); InitWindows();
  461.     InitMenus(); TEInit(); InitDialogs(nil); InitCursor();
  462.     SetPort(qd.thePort);
  463.     GXEnterGraphics();
  464.  
  465.     SetGraphicsLibraryErrors();
  466.     
  467.     gxPostScriptImageDataRec    imageData;
  468.     gxMapping                             devMapping;
  469.     
  470.     // Initialize the input parameters.
  471.     ResetMapping(&devMapping);
  472.     ScaleMapping(&devMapping, FloatToFixed(300.0/72.0), FloatToFixed(300.0/72.0), 0, 0);
  473.     
  474.     if (DoSettingsDialog(&imageData)) {
  475.     
  476.         gxShape            aShape = ShapeRead();            // Read in a GX shape from a file.
  477.         short                refNum = DoSave();                // Create the PostScript file.
  478.         TFontDbase        theDbase = nil;                        // Nil font database causes page by page font handling.
  479.         
  480.         try {
  481.             testDevice        theDevice(refNum);
  482.             if (aShape != nil) {    
  483.                 try {    
  484.  
  485.                     /* Now image the page.  Note, pass in nil for the font database to cause page by page font handling.*/
  486.                     
  487.                     // Construct the translator object.    
  488.                     CGXtoPostScriptTranslator    theTranslator(&theDevice, &imageData, theDbase, &devMapping, nil);
  489.                     
  490.                     // Embed the proc-sets in the PostScript strea.
  491.                     theTranslator.DownloadProcSets();
  492.                     
  493.                     // Normally the LW driver would take care of this during page-setup time.
  494.                     theDevice.BufferData("0 700 translate 1 -1 scale\n", 27, 0);
  495.                     
  496.                     // Image the page.
  497.                     theTranslator.InitGraphics(&imageData, &devMapping, nil);
  498.                         theTranslator.DrawShape(aShape, true);
  499.                     theTranslator.RestoreGraphics();
  500.  
  501.                     // Normally the LW driver would take care of this at the end of the page.
  502.                     theDevice.BufferData("showpage\n", 9, 0);
  503.                     
  504.                 } catch(OSErr status) {
  505.                     printf("Error happend: %d\n", status);
  506.                 }// try-catch.
  507.  
  508.             }//end if
  509.  
  510.             // Close the file            
  511.             SetEOF(refNum, theDevice.GetSize());
  512.             FSClose(refNum);
  513.             
  514.         } catch(OSErr theError) {
  515.             printf("Error happend openning PS file: %d\n", theError);
  516.         }//end try-catch
  517.  
  518.     }//end if    
  519. }
  520.